# -*- coding: utf-8 -*-
from __future__ import print_function
import redis

import ddl_platform.common.settings as settings


class MessageQueue:
    r"""The message queue for the job manager and the resource manager.
    """
    def __init__(self, name, host='localhost', port=6379):
        self._queue_name = name
        self._host = host
        self._port = port
        self._comm  = redis.StrictRedis(host=self._host, port=self._port, charset='utf-8', decode_responses=False, db=settings.REDIS_DB_INDEX)

    def put(self, msg):
        r"""Put the msg the msg queue.

        Args:
            msg (object): The message. 
        """
        self._comm.rpush(self._queue_name, msg)

    def get(self):
        r"""Get the msg from the queue.

        Returns:
            msg (object): The message.
        """
        msg = self._comm.rpop(self._queue_name)
        return msg
